package thread_ex;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.event.MouseEvent;
import java.util.Random;

import javax.swing.JPanel;

public class Ball extends Thread {
	Color color;
	JPanel panel;
	Point point;
	Random r = new Random();



	int size = 50;
	int sizer = r.nextInt(150); 
	int half = size/2;
	int mx = 10;
	int my = -10;

	int x,y;

	int mouseX;

	public void mouseClicked(MouseEvent me)	{
		mouseX = me.getX();		
	}

	public Ball(JPanel p, Point point){
		this.panel=p;
		this.point=point;

	}

	@Override
	public void run(){
		x = (int)point.getX()-half;
		y = (int)point.getY()-half;
		Graphics g = panel.getGraphics();
		g.setColor(Color.WHITE);

		/*g.drawRect((int)point.getX()-half, (int)point.getY()-half, size, size);
		g.setColor(Color.BLACK);
		g.fillOval((int)point.getX()-half, (int)point.getY()-half, size, size);
		g.drawLine(0, 0, (int)point.getX(), (int)point.getY());*/
		color = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));
		/* Polygon star = new Polygon(
				    new int[]{0,22,95,36,59,0,-59,-36,-95,-22},
				    new int[]{-100,-31,-31,12,81,38,81,12,-31,-31},10); // 별 모양 
		 */
		/*while(true){
			g.setColor(Color.white);
			g.drawOval(x, y, size, size);
			x+=mx;
			y+=my;

			if(x<=10){x=10;	mx =10;}
			if(y<=10){y=10; my=10;}
			if(x>=panel.getWidth()-60){
				x=panel.getWidth()-60;
				mx=-10;}
			if(y>=panel.getHeight()-60){
				y=panel.getHeight()-60;
				my=-10;
				}

			g.setColor(color);
			g.drawOval(x, y, size, size);

			try {
				Thread.sleep(100);
			} catch (Exception e) {

			}			
		}*/

		while(true){
			g.setColor(Color.WHITE);
			g.fillOval(x, y, sizer, sizer);
			
			x += mx;
			y += my;
			if(x <= 10){
				x=10;
				mx=10;				
			}
			if(y <= 10){
				y=10;
				my=10;
			}
			if(x >= panel.getWidth()-50){
				x=panel.getWidth()-50;
				mx=-10;
			}
			if(y >= panel.getHeight()-50){
				y=panel.getHeight()-50;
				my=-10;
			}



			g.setColor(color);
			g.fillOval(x, y, sizer, sizer);
			
			try{
				Thread.sleep(50);
			}catch(Exception ex){

			}

		}


	}
}
